package com.yidi.system.service.imp;

import com.yidi.system.domain.SysUserRole;
import com.yidi.system.repository.SysUserRoleRepository;
import com.yidi.system.service.SysUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author ：Xn
 * @ClassName: SysUserRoleServiceImpl
 * @date ：2021-10-19 14:21
 * @describe ：
 */
@Service
public class SysUserRoleServiceImpl implements SysUserRoleService {

    @Autowired
    private SysUserRoleRepository sysUserRoleRepository;

    @Override
    public int countUserRoleByRoleId(Long roleId) {
        return sysUserRoleRepository.countUserRoleByRoleId(roleId);
    }

    @Override
    public void deleteUserRoleInfo(Long userId, Long roleId){
        sysUserRoleRepository.deleteUserRoleInfo(userId, roleId);
    }

    @Override
    public void add(SysUserRole ur) {
        sysUserRoleRepository.save(ur);
    }

    /**
     * 通过用户ID删除用户和角色关联
     *
     * @param userId 用户ID
     * @return 结果
     */
    @Override
    public void deleteUserRoleByUserId(Long userId) {

        sysUserRoleRepository.deleteUserRoleByUserId(userId);
    }

    /**
     * 批量新增用户角色信息
     *
     * @param userRoleList 用户角色列表
     * @return 结果
     */
    @Override
    public void batchUserRole(List<SysUserRole> userRoleList) {
        for(SysUserRole ur : userRoleList) {
            add(ur);
        }
    }

    @Override
    public void deleteUserRole(Long[] userIds){
        for(Long id : userIds){
            deleteUserRoleByUserId(id);
        }
    }

    @Override
    public List<SysUserRole> findByUserId(Long userId) {
        return sysUserRoleRepository.findByUserId(userId);
    }
}
